class DiskScheduler {
  constructor(currentPosition, lastPosition, requestQueue) {
    // 当前位置
    this.currentPosition = currentPosition
    // 上一次位置
    this.lastPosition = lastPosition
    // 请求队列
    this.requestQueue = requestQueue
  }

  // FCFS算法
  calculateFCFS() {
    const path = [this.currentPosition, ...this.requestQueue]
    let totalDistance = 0

    for (let i = 0; i < path.length - 1; i++) {
      totalDistance += Math.abs(path[i + 1] - path[i])
    }

    return {
      path,
      totalDistance,
      averageDistance:
        this.requestQueue.length > 0
          ? totalDistance / this.requestQueue.length
          : 0
    }
  }

  // SSTF算法
  calculateSSTF() {
    let current = this.currentPosition
    let remaining = [...this.requestQueue]
    let path = [current]
    let totalDistance = 0

    // 选择距离当前位置最近的请求
    while (remaining.length > 0) {
      const distances = remaining.map((pos) => Math.abs(pos - current))
      const minDistance = Math.min(...distances)
      const nearestIdx = distances.indexOf(minDistance)

      current = remaining[nearestIdx]
      path.push(current)
      totalDistance += minDistance
      remaining.splice(nearestIdx, 1)
    }

    return {
      path,
      totalDistance,
      averageDistance:
        this.requestQueue.length > 0
          ? totalDistance / this.requestQueue.length
          : 0
    }
  }

  // 电梯调度算法
  calculateElevator() {
    const direction = this.currentPosition < this.lastPosition ? 'down' : 'up'
    let current = this.currentPosition
    let remaining = [...this.requestQueue]
    let path = [current]
    let totalDistance = 0

    // 添加与当前位置相同的请求，可能有多个
    const sameRequests = remaining.filter((r) => r === current)
    path.push(...sameRequests)

    // 分别处理向上和向下的请求
    const upRequests = remaining
      .filter((r) => r > current)
      .sort((a, b) => a - b)
    const downRequests = remaining
      .filter((r) => r < current)
      .sort((a, b) => b - a)

    // 根据当前方向选择先处理的请求队列
    const primaryRequests = direction === 'up' ? upRequests : downRequests
    const secondaryRequests = direction === 'up' ? downRequests : upRequests

    // 先处理主方向的请求
    for (const target of primaryRequests) {
      const distance = Math.abs(target - current)
      totalDistance += distance
      current = target
      path.push(current)
    }

    // 再处理反方向的请求
    for (const target of secondaryRequests) {
      const distance = Math.abs(target - current)
      totalDistance += distance
      current = target
      path.push(current)
    }

    return {
      path,
      totalDistance,
      averageDistance:
        this.requestQueue.length > 0
          ? totalDistance / this.requestQueue.length
          : 0
    }
  }

  calculate(algorithmType) {
    switch (algorithmType) {
      case 'fcfs':
        return this.calculateFCFS()
      case 'sstf':
        return this.calculateSSTF()
      case 'elevator':
        return this.calculateElevator()
      default:
        return { path: [], totalDistance: 0, averageDistance: 0 }
    }
  }
}

export default DiskScheduler
